package cn.cdqf.coupon.common;

import cn.cdqf.globle.exception.GlobleException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;
import java.util.stream.Stream;

import static cn.cdqf.globle.common.GlobleConstant.EnumConstant.NO_MATCH_INSTANCE;

@Getter
@AllArgsConstructor
public enum  CouponLevel {

    FIRST_LEVEL(1,0,20),
    SECOND_LEVEL(2,20,50),
    THIRD_LEVEL(3,50,100),
    OTHER_LEVEL(4,100,Integer.MAX_VALUE),;
    private Integer level;

    private Integer begin;

    private Integer end;

    public static CouponLevel getCouponLevel(Integer level){
        //可以拿到所有的枚举对象
        CouponLevel[] values = values();
        Optional<CouponLevel> first = Stream.of(values).filter(couponLevel -> couponLevel.getLevel() == level).findFirst();
        if(first.isPresent()){
            return first.get();
        }
        throw new GlobleException(NO_MATCH_INSTANCE+level);
    }
}
